﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MidApp
{
    /// <summary>
    ///Trida spravujici akce.
    ///</summary>
    public class ManageEvent
    {
        /// <summary>
        ///Metoda vlozi novou udalost do databaze.
        ///</summary>
        public static void CreateNewEvent(AppLib.Event newevent,int TeamID, bool SignTeamMembers)
        {
            var db = MidApp.LinqUse.DBConn;

            AppLib.Event sum = new AppLib.Event();
            int EventID;

            db.Events.InsertOnSubmit(newevent);
            db.SubmitChanges();

            EventID = newevent.EventID;
            //+ prirazeni vytvarene akce k tymu
            MidApp.ManagePossession.AssignEventToTeam(GetEventByID(EventID).EventID, TeamID);

            if (SignTeamMembers)
            {
                var aux = MidApp.ManageIsMember.GetAllMembers(TeamID);

                foreach (var item in aux)
                {
                    AppLib.Detail newDetail = new AppLib.Detail();
                    newDetail.Amount = "-" + MidApp.ManageEvent.GetEventByID(EventID).Price.ToString();
                    newDetail.Detail1 = "Strhnuto za přihlášení na akci " + MidApp.ManageEvent.GetEventByID(EventID).EventName;
                    newDetail.Date = DateTime.Today;
                    ManageParticipate.SignUserToEvent(EventID, item.UserID, TeamID, newDetail);
                }
             }
            
        }
        /// <summary>
        ///Metoda vraci kolekci akci serazenych sestupne dle data konani.
        ///</summary>
        public static List<AppLib.Event> GetAllEventsOrderedDescending(int UserID)
        {
            var db = MidApp.LinqUse.DBConn;

            return (from Event in db.Events join Possession in db.Possessions on Event.EventID equals Possession.EventID into EP/*EventPossession*/ from ep in EP join IsMember in db.IsMembers on ep.TeamID equals IsMember.TeamID where IsMember.UserID == UserID orderby Event.DateOfHolding descending select Event).ToList();
        }
        /// <summary>
        ///Metoda vraci kolekci akci ve vyhledu do budoucna od dnesniho data, serazeno sestupne dle data konani.
        ///</summary>
        public static List<AppLib.Event> GetActualEventsOrderedAscending(int UserID)
        {
            var db = MidApp.LinqUse.DBConn;

            return (from Event in db.Events join Possession in db.Possessions on Event.EventID equals Possession.EventID into EP/*EventPossession*/ from ep in EP join IsMember in db.IsMembers on ep.TeamID equals IsMember.TeamID where IsMember.UserID == UserID && Event.DateOfHolding.Date.CompareTo(System.DateTime.Now.Date) >= 0 orderby Event.DateOfHolding ascending select Event).ToList();
        }
        /// <summary>
        ///Metoda vraci kolekci akci, ktere jiz probehly, serazenych sestupne dle data konani.
        ///</summary>
        public static List<AppLib.Event> GetPastEventsOrderedDescending(int UserID)
        {
            var db = MidApp.LinqUse.DBConn;

            return (from Event in db.Events join Possession in db.Possessions on Event.EventID equals Possession.EventID into EP/*EventPossession*/ from ep in EP join IsMember in db.IsMembers on ep.TeamID equals IsMember.TeamID where IsMember.UserID == UserID && Event.DateOfHolding.Date.CompareTo(System.DateTime.Now.Date) < 0 orderby Event.DateOfHolding descending select Event).ToList();
        }
        /// <summary>
        ///Metoda vraci kolekci akci, na ktere je prihlasen hrac.
        ///</summary>
        public static List<AppLib.Event> GetHisEventsOrderedDescending(int UserID)
        {
            var db = MidApp.LinqUse.DBConn;

            return (from Event in db.Events join Participate in db.Participates on Event.EventID equals Participate.EventID into EP/*EventParticipate*/ from ep in EP join IsMember in db.IsMembers on ep.TeamID equals IsMember.TeamID where IsMember.UserID == UserID && ep.UserID == UserID orderby Event.DateOfHolding descending select Event).ToList();

        }
        /// <summary>
        ///Metoda vraci kolekci akci, ktere patri tymu.
        ///</summary>
        public static List<AppLib.Event> GetAllEventsByTeamOrderedDescending(int TeamID)
        {
            var db = MidApp.LinqUse.DBConn;

            return (from Event in db.Events join Possession in db.Possessions on Event.EventID equals Possession.EventID where Possession.TeamID==TeamID orderby Event.DateOfHolding descending select Event).ToList();

        }
        /// <summary>
        ///Metoda vlozi nove udalosti do databaze.
        ///</summary>
        public static void CreateNewEvents(Stack<AppLib.Event> stack, int TeamID, bool SignTeamMembers)
        {
           for (int i = 1; i <= stack.Count; i++) 
           {
               AppLib.Event e = stack.ElementAt(i-1);
               CreateNewEvent(e, TeamID,SignTeamMembers);
           }
        
        }
        /// <summary>
        ///Metoda vraci udalost se specifickym ID.
        ///</summary>
        public static AppLib.Event GetEventByID(int EventID)
        {
            var db = MidApp.LinqUse.DBConn;
            return (from Event in db.Events where Event.EventID == EventID select Event).First();
        }
        /// <summary>
        ///Metoda vraci udalosti podle vypisovatele.
        ///</summary>
        public static List<AppLib.Event> GetEventsByAuthor(string Nick)
        {
            var db = MidApp.LinqUse.DBConn;

            return (from Event in db.Events where Event.CreatedBy == Nick orderby Event.DateOfHolding descending select Event).ToList();
        }
        /// <summary>
        ///Metoda vraci udalosti pro tym podle vypisovatele.
        ///</summary>
        public static List<AppLib.Event> GetEventsByAuthorForTeam(string Nick, int TeamID)
        {
            var db = MidApp.LinqUse.DBConn;

            return (from Event in db.Events join Possession in db.Possessions on Event.EventID equals Possession.EventID where Possession.TeamID==TeamID && Event.CreatedBy==Nick orderby Event.DateOfHolding descending select Event).ToList();
        }
        /// <summary>
        ///Metoda slouzi k editaci akce.
        ///</summary>
        public static void AssignNewEventInfo(AppLib.Event EventToEdit)
        {                     
            var db = MidApp.LinqUse.DBConn;
            var oldEvent = (from Event in db.Events where Event.EventID == EventToEdit.EventID select Event).First();

            oldEvent.Address = EventToEdit.Address;
            oldEvent.EventName = EventToEdit.EventName;
            oldEvent.DateOfHolding = EventToEdit.DateOfHolding;
            oldEvent.Description = EventToEdit.Description;
            oldEvent.Price = EventToEdit.Price;
            oldEvent.Seats = EventToEdit.Seats;

            db.SubmitChanges();

        
        }
    }
}
